#coding:utf-8

import re
import urllib2
import urllib
import os

#url = 'http://www.ibtimes.com.cn/'
url = 'http://www.qiushibaike.com/imgrank/'
hander = {
        "Referer":"http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-4939841000086153&format=728x90&output=html&h=90&slotname=5196507409&adk=3501348400&adf=1745964834&w=728&lmt=1487907721&flash=18.0.0&url=http%3A%2F%2Fwww.qiushibaike.com%2Fimgrank%2F&wgl=1&dt=1487907721153&bpp=20&bdt=2842&fdt=24&idt=31&shv=r20170220&cbv=r20170110&saldr=aa&correlator=4762664972894&frm=20&ga_vid=364375586.1487907700&ga_sid=1487907721&ga_hid=2134433666&ga_fc=0&pv=2&iag=3&icsg=2&nhd=1&dssz=2&mdo=0&mso=0&u_tz=480&u_his=3&u_java=0&u_h=900&u_w=1600&u_ah=860&u_aw=1600&u_cd=24&u_nplug=29&u_nmime=58&dff=pinghei&dfs=14&adx=428&ady=12993&biw=1583&bih=731&oid=3&ref=http%3A%2F%2Fwww.qiushibaike.com%2Fpic%2F&rx=0&eae=0&fc=16&brdim=0%2C109%2C0%2C109%2C1600%2C0%2C1600%2C731%2C1600%2C731&vis=1&rsz=%7C%7CeEbr%7C&abl=CS&ppjl=t&pfx=0&fu=8&bc=1&ifi=1&xpc=ELRI227n7v&p=http%3A//www.qiushibaike.com&dtd=57",
        "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.22 Safari/537.36 SE 2.X MetaSr 1.0"
        }

def ppk(url):
    req = urllib2.Request(url,None,hander)
    opens = urllib2.urlopen(req)
    content = opens.read()
    img = re.findall(r'<img src="(.*?)"',content,re.S)
    if not os.path.exists('e:/tmp'):
        os.makedirs('e:/tmp')
    for i in img:
        try:
            name = i.rsplit('/',1)[1]
            name = "E:\\tmp\\" + name
            urllib.urlretrieve(i,name)
            print name,'is done'
        except:
            pass
ppk(url)



